package com.atguigu.gmall.payment.service.impl;

import com.atguigu.gmall.payment.mapper.PaymentInfoMapper;
import com.atguigu.gmall.payment.model.PaymentInfo;
import com.atguigu.gmall.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper,PaymentInfo> implements PaymentInfoService {

    /**
     * 保存本地交易记录
     * @param paymentInfo 支付对象
     * @param paymentType 支付类型
     */
    @Override
    public void savePaymentInfo(PaymentInfo paymentInfo,String paymentType) {
        //1.判断用户当前订单产生本地交易记录是否存在
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderId, paymentInfo.getOrderId());
        queryWrapper.eq(PaymentInfo::getPaymentType, paymentType);
        PaymentInfo payInfo = this.getOne(queryWrapper);
        if(payInfo != null){
            return;
        }
        //2.将当前用户交易记录保存到本地
        this.save(paymentInfo);
    }

    /**
     * 根据订单编号+支付方式查询本地交易记录
     * @param outTradeNo 订单编号
     * @param paymentType 支付方式
     * @return 本地交易记录
     */
    @Override
    public PaymentInfo getPaymentInfoByOutTradeNo(String outTradeNo, String paymentType) {
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOutTradeNo, outTradeNo);
        queryWrapper.eq(PaymentInfo::getPaymentType, paymentType);
        return this.getOne(queryWrapper);
    }

    /**
     * 根据订单编号+支付方式,查询本地交易记录
     * @param orderId 订单id
     * @param paymentType 支付方式
     * @return 本地交易记录
     */
    @Override
    public PaymentInfo getPaymentInfoByOrderId(Long orderId, String paymentType) {
        LambdaQueryWrapper<PaymentInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PaymentInfo::getOrderId, orderId);
        queryWrapper.eq(PaymentInfo::getPaymentType, paymentType);
        PaymentInfo paymentInfo = this.getOne(queryWrapper);
        return paymentInfo;
    }
}